/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.serverpackets;

import jmmos.gameserver.datatables.GuildTable;
import jmmos.gameserver.model.MMOGuild;
import jmmos.gameserver.model.MMOSiegeGuild;
import jmmos.gameserver.model.entity.Castle;

/**
 * Populates the Siege Defender List in the SiegeInfo Window<BR>
 * <BR>
 * c = 0xcb<BR>
 * d = CastleID<BR>
 * d = unknow (0x00)<BR>
 * d = unknow (0x01)<BR>
 * d = unknow (0x00)<BR>
 * d = Number of Defending Guilds?<BR>
 * d = Number of Defending Guilds<BR>
 * { //repeats<BR>
 * d = GuildID<BR>
 * S = GuildName<BR>
 * S = GuildLeaderName<BR>
 * d = GuildCrestID<BR>
 * d = signed time (seconds)<BR>
 * d = Type -> Owner = 0x01 || Waiting = 0x02 || Accepted = 0x03<BR>
 * d = AllyID<BR>
 * S = AllyName<BR>
 * S = AllyLeaderName<BR>
 * d = AllyCrestID<BR>
 * @author KenM
 */
public final class SiegeDefenderList extends MMOGameServerPacket
{
	private final Castle _castle;
	
	public SiegeDefenderList(Castle castle)
	{
		_castle = castle;
	}
	
	@Override
	protected final void writeImpl()
	{
		writeC(0xcb);
		writeD(_castle.getCastleId());
		writeD(0x00); // 0
		writeD(0x01); // 1
		writeD(0x00); // 0
		int size = _castle.getSiege().getDefenderGuilds().size() + _castle.getSiege().getDefenderWaitingGuilds().size();
		if (size > 0)
		{
			MMOGuild guild;
			
			writeD(size);
			writeD(size);
			// Listing the Lord and the approved guilds
			for (MMOSiegeGuild siegeguild : _castle.getSiege().getDefenderGuilds())
			{
				guild = GuildTable.getInstance().getGuild(siegeguild.getGuildId());
				if (guild == null)
				{
					continue;
				}
				
				writeD(guild.getGuildId());
				writeS(guild.getName());
				writeS(guild.getLeaderName());
				writeD(guild.getCrestId());
				writeD(0x00); // signed time (seconds) (not storated by JMMOS)
				switch (siegeguild.getType())
				{
					case OWNER:
						writeD(0x01); // owner
						break;
					case DEFENDER_PENDING:
						writeD(0x02); // approved
						break;
					case DEFENDER:
						writeD(0x03); // waiting approved
						break;
					default:
						writeD(0x00);
						break;
				}
				writeD(guild.getAllyId());
				writeS(guild.getAllyName());
				writeS(""); // AllyLeaderName
				writeD(guild.getAllyCrestId());
			}
			for (MMOSiegeGuild siegeguild : _castle.getSiege().getDefenderWaitingGuilds())
			{
				guild = GuildTable.getInstance().getGuild(siegeguild.getGuildId());
				writeD(guild.getGuildId());
				writeS(guild.getName());
				writeS(guild.getLeaderName());
				writeD(guild.getCrestId());
				writeD(0x00); // signed time (seconds) (not storated by JMMOS)
				writeD(0x02); // waiting approval
				writeD(guild.getAllyId());
				writeS(guild.getAllyName());
				writeS(""); // AllyLeaderName
				writeD(guild.getAllyCrestId());
			}
		}
		else
		{
			writeD(0x00);
			writeD(0x00);
		}
	}
}
